package Action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import Beans.Admin;
import Beans.Card;
import DAO.AdminDao;
import DAO.CardDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {

	private Admin admin = new Admin();

	private int adminID;
	private int cardID;
	private List<Admin> admins;
	private List<Card> cards;
	private String returnType;
	private String loginName;
	private String funId;
	private String msg;
	private String loginPsd;
	Map<String, Object> message;

	CardDao cardDao = new CardDao();

	public Admin getModel() {
		return admin;
	}

	private AdminDao adminDao = new AdminDao();

	/**
	 * 货架管理员登陆
	 * 
	 * @return 
	 * http://localhost/YouBoxAdmin/adminLogin?loginName=admin&loginPsd=admin&funId=6666
	 * @throws Exception
	 */
	public String adminLogin() throws Exception {
		admins = adminDao.adminLogin(loginName, loginPsd);
		if ("6666".equals(funId) && admins.size() == 1) {
			msg = "success:1";
		} else {
			msg = "error:-1";
		}
		ServletActionContext.getResponse().getWriter().print(msg);
		return null;
		/*
		 * if (returnType == null) {
		 * 
		 * return "success"; } else if (returnType.equals("json")) {
		 * 
		 * 
		 * }
		 */
	}

	/**
	 * 获取管理员授权卡
	 * 
	 * @return
	 */
	public String getAdminCard() {
		cards = cardDao.getAdminCard();
		return "success";
	}

	/**
	 * 获取管理员信息
	 * 
	 * @return
	 */
	public String getAdminById() {
		admin = adminDao.getAdminById(adminID);
		String sernum = admin.getSernum();
		if (sernum != null) {
			Card card = cardDao.getSernumCard(sernum).get(0);
			cardDao.change(card, 0);
		}
		cards = cardDao.getAdminCard();
		return "success";
	}
	
	public String getByAdminId() {
		admin = adminDao.getAdminById(adminID);
		admins = new ArrayList<Admin>();
		admins.add(admin);
		return "success";
	}

	/**
	 * 编辑管理员
	 * 
	 * @return
	 */
	public String editAdmin() {
		Card card = cardDao.getCardById(cardID);
		admin.setAdminId(adminID);
		admin.setRfid(card.getRfid());
		admin.setSernum(card.getSernum());
		cardDao.change(card, 1);
		adminDao.edit(admin);
		return "success";
	}

	/**
	 * 删除管理员
	 * 
	 * @return
	 */
	public String deleteAdmin() {
		admin = adminDao.getAdminById(adminID);
		String sernum = admin.getSernum();
		if (sernum != null) {
			Card card = cardDao.getSernumCard(sernum).get(0);
			cardDao.change(card, 0);
		}
		adminDao.delete(admin);
		return "success";
	}

	/**
	 * Model-driven to fetch data So the method name have to be getModel()
	 * 
	 * @return
	 */

	public String login() {

		Admin admin1 = adminDao.get(admin.getUsername(), admin.getPassword());
		if (returnType == null) {
			if (admin1 != null) {
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("admin", admin1);
				session.setMaxInactiveInterval(3600);	
				return "success";
			} else {
				return "fail";
			}
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			if (admin1 != null) {

				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("admin", admin1);
				session.setMaxInactiveInterval(3600);	

				message.put("result", 0);

			} else {

				message.put("result", 1);

			}
			return "jsonResult";
		} else {
			return "fail";
		}

	}

	public String registerAdmin() {
		Card card = cardDao.getCardById(cardID);
		admin.setRfid(card.getRfid());
		admin.setSernum(card.getSernum());
		cardDao.change(card, 1);
		adminDao.save(admin);
		return "success";
	}

	public String getAll() {

		admins = adminDao.getAll();
		if (returnType == null) {

			return "success";
		} else if (returnType.equals("json")) {
			message = new HashMap<String, Object>();
			message.put("result", 0);
			message.put("admins", admins);
			return "jsonSuccess";
		}
		return "success";

	}

	public List<Admin> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public Map<String, Object> getMessage() {
		return message;
	}

	public void setMessage(Map<String, Object> message) {
		this.message = message;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public int getAdminID() {
		return adminID;
	}

	public void setAdminID(int adminID) {
		this.adminID = adminID;
	}

	public int getCardID() {
		return cardID;
	}

	public void setCardID(int cardID) {
		this.cardID = cardID;
	}

	public List<Card> getCards() {
		return cards;
	}

	public void setCards(List<Card> cards) {
		this.cards = cards;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPsd() {
		return loginPsd;
	}

	public void setLoginPsd(String loginPsd) {
		this.loginPsd = loginPsd;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}
